//
//  QTFamousChildController.m
//  QTLifeCircle
//
//  Created by 王琼 on 17/1/17.
//  Copyright © 2017年 徐腾. All rights reserved.
/*
   0.将当前控制器和sb绑定
   1.先请求数据
   2.设置好数据源,代理
   3.写好模型类
   4.绑定原型cell(注意sb和纯代码敲的区别)
   5.通过接受到的数据,给cell赋值
 
 */

#import "QTFamousChildController.h"
#import "QTLifeCircleNetworkingTool.h"
#import "QTBuessinessHeaderView.h"
#import "QTBusinessCommentCell.h"
#import "QTBuessinessGoodsCell.h"
#import "QTBuessinessScoverCell.h"
#import "QTFamousMapController.h"
#import <YYModel.h>
//导入数据源头文件
#import "QTFmousModel.h"
#import "QTUserCommentModel.h"
#import "QTGoodsModel.h"
@interface QTFamousChildController ()<UITableViewDataSource,UITableViewDelegate,QTBuessinessHeaderViewDelegate>
@property (weak, nonatomic) IBOutlet QTBuessinessHeaderView *headerView;

@end

@implementation QTFamousChildController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    self.headerView.model = self.famousModel;
    //将代理设置给view
    self.headerView.delegate = self;
    self.tableView.showsVerticalScrollIndicator = NO;
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 请求数据

-(void)loadData
{
    NSDictionary* dict = @{@"shop_id":@"0/1/2/3"};
    [[QTLifeCircleNetworkingTool sharedManager] POSTWithURLStr:@"http://iosapi.itcast.cn/life/shop_info.json.php" parameter:dict compeletionHandle:^(id responseObject) {
        
        
        
        //表头是这个大字典里的key的值
        //拿到用户评论
        NSArray* userCommentArray = responseObject[@"comment_list"];
        NSMutableArray* commentModelarr = [NSMutableArray array];
        for (NSDictionary* dict in userCommentArray)
        {
            QTUserCommentModel* commentModel = [QTUserCommentModel yy_modelWithDictionary:dict];
            [commentModelarr addObject: commentModel];
        }
        self.userCommentList = commentModelarr;
        [self.tableView reloadData];
       
        
        //拿到商品详情
        NSArray* goodsArray = responseObject[@"goods"];
        NSMutableArray* goodsMoldelArr = [NSMutableArray array];
        for (NSDictionary* dict in goodsArray)
        {
            QTGoodsModel* goodsModel = [QTGoodsModel yy_modelWithDictionary:dict];
            [goodsMoldelArr addObject:goodsModel];
        }
        self.goodsList = goodsMoldelArr;
        [self.tableView reloadData];
    
    }];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if(section == 0)
    {
        return self.userCommentList.count;
        
    }else if(section == 1)
    {
        return self.goodsList.count;
    }else
    {
        return 1;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.section == 0)
    {
      QTBusinessCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell" forIndexPath:indexPath];
        cell.model = self.userCommentList[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if(indexPath.section == 1)
    {
        QTBuessinessGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"companyCell1" forIndexPath:indexPath];
        cell.model = self.goodsList[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else
    {
       
        QTBuessinessScoverCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

}

//返回组高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0)
    {
        return 100;
        
    }else if(indexPath.section == 1)
    {
        return 250;
    }else
    {
        return 200;
    }
}

//返回组标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return @"用户评论";
        
    }else if(section == 1)
    {
        return @"公司商品";
    }else
    {
       return @"评价";
    }

}

#pragma mark -QTBuessinessHeaderViewDelegate

-(void)pushToController:(NSString *)btn
{
    //    QTFamousChildController* conVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil]instantiateViewControllerWithIdentifier:@"ChildControll"];
    //    //在这里将数据传给控制器
    //    //分析:这里将indexPath.item的model
    //    conVC.famousModel = model;
    //    [self.navigationController pushViewController:conVC animated:YES];
    //
    QTFamousMapController* mapVC =[[UIStoryboard storyboardWithName:@"Home" bundle:nil]instantiateViewControllerWithIdentifier:@"mapConreoller"];
    //这里设置大头针的标题还有地图定位的地址名
    mapVC.addressStr = btn;
    //po 了一下,在这里是拿得到值的
    [self.navigationController pushViewController:mapVC animated:YES];
    
}


@end
